import jwt from 'jsonwebtoken'
import appConfig from '../configs/app'

export default {
  // 生成token
  sign(data) {
    return jwt.sign(data, appConfig.tokenSecretKey, { expiresIn: appConfig.tokenExpires })
  },
  // 从token获取数据
  verify(token) {
    let result = {
      data: null,
      success: false
    }
    try {
      result = {
        data: jwt.verify(token, appConfig.tokenSecretKey),
        success: true
      }
    } catch (err) {
      console.log('-- error -- token 验证失败')
    } finally {
      return result
    }
  }
}
